#pragma once
#include <iostream>
#include <mutex>
#include <string>
#include <vector>
#include "log.hpp"
#include <fstream>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"

namespace ns_util
{
	class FileUtil
	{
	public:
		// 读文件
		static bool ReadFile(const std::string &path, std::string *text)
		{
			std::ifstream in(path, std::ios::in);
			if (!in.is_open())
			{
				std::cerr << "open file error" << std::endl;
				return false;
			}

			std::string line;
			while (std::getline(in, line))
			{
				*text += line;
			}

			in.close();
			return true;
		}

	};

	class StringUtil
	{
	public:
		static void StringCut(const std::string &line, std::vector<std::string> *result, const std::string &sep)
		{
			boost::split(*result, line, boost::is_any_of(sep), boost::token_compress_on);
		}
	};

	const char *const DICT_PATH = "./dict/jieba.dict.utf8";
	const char *const HMM_PATH = "./dict/hmm_model.utf8";
	const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
	const char *const IDF_PATH = "./dict/idf.utf8";
	const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";

	// 去除暂停词
	// class JiebaUtil
	// {
	// private:
	// 	cppjieba::Jieba jieba;
	// 	std::unordered_map<std::string, bool> stop_words;
	// 	JiebaUtil():jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH){

	// 	}

	// 	JiebaUtil(const JiebaUtil&) = delete;

	// 	static JiebaUtil* instance;

	// public:

	// 	static JiebaUtil* Get_instance(){
	// 		static std::mutex mtx;
	// 		if(instance == nullptr){
	// 			mtx.lock();
	// 			if(instance == nullptr){
	// 				instance = new JiebaUtil();
	// 				instance->InitJiebaUtil();
	// 			}
	// 			mtx.unlock();
	// 		}
	// 		return instance;
	// 	}

	// 	void InitJiebaUtil(){
	// 		std::ifstream in(STOP_WORD_PATH);
	// 		if(!in.is_open()){
	// 			LOG(FATAL, "open file error");
	// 			return;
	// 		}

	// 		std::string line;
	// 		while(std::getline(in, line)){
	// 			stop_words.insert({line, true});
	// 		}

	// 		in.close();
	// 	}

	// 	void CutStringHelper(const std::string &src, std::vector<std::string> *out)
	// 	{
	// 		jieba.CutForSearch(src, *out);
	// 		for (auto iter = out->begin(); iter != out->end(); )
	// 		{
	// 			auto it = stop_words.find(*iter);
	// 			if(it != stop_words.end()){
	// 				iter = out->erase(iter);
	// 			}
	// 			else{
	// 				++iter;
	// 			}
	// 		}
	// 	}

	// 	static void CutString(const std::string &src, std::vector<std::string> *out)
	// 	{
	// 		ns_util::JiebaUtil::Get_instance()->CutStringHelper(src, out);
	// 	}

		
	// };

	// JiebaUtil* JiebaUtil::instance = nullptr;

	class JiebaUtil
	{
	private:
		static cppjieba::Jieba jieba;

	public:
		static void CutString(const std::string &src, std::vector<std::string> *out)
		{
			jieba.CutForSearch(src, *out);
		}
	};

	cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
}
